package com.fusionride.wellness.config.mqtt.consumer;


import com.fusionride.wellness.service.consumer.MqttMsgConsumer;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;

/**
 * 获取配置参数
 */
@Configuration
@IntegrationComponentScan
public class MqttConsumerConfig {
    @Value("${mqtt.brokerURL}")
    private String url;
    @Value("${mqtt.clientId}")
    private String clientId;
    @Value("${mqtt.userName}")
    private String username;
    @Value("${mqtt.password}")
    private String password;
    @Value("${mqtt.connectionTimeout}")
    private Integer timeout;
    @Value("${mqtt.keepAliveInterval}")
    private Integer keepalive;
    @Value("${mqtt.v2x.device.upPre}")
    private String topics;

    /**
     * 设置mqtt的连接工厂
     *
     * @return
     */
    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        String[] array = this.url.split(",");
        MqttConnectOptions options = new MqttConnectOptions();
        options.setServerURIs(array);
        options.setUserName(this.username);
        options.setPassword(this.password.toCharArray());
        options.setConnectionTimeout(this.timeout);
        options.setKeepAliveInterval(this.keepalive);
        options.setCleanSession(false);
        options.setAutomaticReconnect(true);
        options.setMaxInflight(1000);

        factory.setConnectionOptions(options);
        return factory;
    }

    /**
     * 设置入站通道
     */
    @Bean
    public MessageChannel mqttInputChannel() {
        return new DirectChannel();
    }

    /**
     * 配置client,监听的topic
     */
    @Bean
    public MessageProducer mqttInbound() {
        //String topic =  "$queue/"+ this.topics;
        //String topic =  "WELLNESS/UP/A";
        String topic =  "HCR/UP/D/22120001";
        String[] inboundTopics = topic.split(",");
        // Paho 客户端消息驱动通道适配器，主要用来订阅主题
        MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(this.clientId + "_inbound", mqttClientFactory(), inboundTopics);  //对inboundTopics主题进行监听
        adapter.setCompletionTimeout(5000);
        // 设置QOS
        adapter.setQos(1);
        // 设置Paho 消息转换器
        DefaultPahoMessageConverter defaultPahoMessageConverter = new DefaultPahoMessageConverter();
        // 如果是字节消息的话，需要设置这个
//        defaultPahoMessageConverter.setPayloadAsBytes(true);
        adapter.setConverter(defaultPahoMessageConverter);
        adapter.setOutputChannel(mqttInputChannel());
        return adapter;
    }

    /**
     * 消费处理mqtt消息
     * ServiceActivator注解表明:当前方法用于处理mqtt消息，inputChannel参数指定了用于消费的channel
     */
    @Bean
    @ServiceActivator(inputChannel = "mqttInputChannel")
    public MessageHandler handler() {
        return new MqttMsgConsumer();
    }
}
